package me.project.jobster.controller;

import com.alibaba.fastjson.JSON;
import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.common.util.BeanUtils;
import me.project.jobster.common.util.Response;
import me.project.jobster.dao.mybatis.mapper.FriendMapper;
import me.project.jobster.dao.mybatis.model.Friend;
import me.project.jobster.dao.mybatis.model.Student;
import me.project.jobster.service.FriendService;
import me.project.jobster.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author Mcdull
 * @date 2018-4-27
 */
@Controller
@RequestMapping("student")
public class StudentController extends BaseController{

    @Autowired
    private UserService userService;
    @Autowired
    private FriendService friendService;

    /**
     * students page
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("index")
    public String index(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "1");
        List<Student> list = userService.getStudentList();

        //remove itself
        String userFlag = String.valueOf(session.getAttribute("user_flag"));
        String userId = String.valueOf(session.getAttribute("user_id"));
        if (GlobalConstants.STUDENT.equals(userFlag)){
            for (Student student : list) {
                if (student.getsId().equals(userId)) {
                    list.remove(student);
                    break;
                }
            }
        }
        model.addAttribute("studentList", list);
        model.addAttribute("student", new Student());
        return "student/students";
    }

    /**
     * search page
     * @param request
     * @param model
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @PostMapping("search")
    public String search(HttpServletRequest request, Model model) throws InvocationTargetException, IllegalAccessException {
        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "1");

        Student student = new Student();
        BeanUtils.copyMap2Bean(student, getParameterMap(request));
        String gpaSearchFlag = request.getParameter("gpaSearchFlag");
        List<Student> list = userService.getStudentListFuzzy(student,gpaSearchFlag);
        model.addAttribute("studentList", list);
        model.addAttribute("student", student);
        return "student/students";
    }

    /**
     * student detail page
     * @param request
     * @param model
     * @return
     */
    @GetMapping("detail")
    public String detail(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        session.setAttribute(GlobalConstants.MENU, "1");
        String userId = request.getParameter("sId");
        if (StringUtils.isEmpty(userId)){
            List<Student> list = userService.getStudentList();
            model.addAttribute("studentList", list);
            model.addAttribute("student", new Student());
            return "student/students";
        }
        Student student = new Student();
        student.setsId(userId);
        Student s = userService.getStudentInfo(userId);
        model.addAttribute("student", s);
        return "student/detail";
    }

    /**
     * student toggle page
     *
     * @param request
     * @return
     */
    @GetMapping("toggle/list")
    @ResponseBody
    public String studentList(HttpServletRequest request) {
        Response response = Response.getInstance("","");
        countMsgUnread(request);

        HttpSession session = request.getSession();
        String userId = String.valueOf(session.getAttribute("user_id"));

        List<Student> list = friendService.getAllFriend(userId);

        response.setResult(list);
        return JSON.toJSONString(response);
    }

}
